{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Self join\n",
    "\n",
    "## Edinburgh Buses\n",
    "[Details of the database](https://sqlzoo.net/wiki/Edinburgh_Buses.) Looking at the data\n",
    "\n",
    "```\n",
    "stops(id, name)\n",
    "route(num, company, pos, stop)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " ·········\n"
     ]
    }
   ],
   "source": [
    "# Prerequesites\n",
    "import getpass\n",
    "%load_ext sql\n",
    "pwd = getpass.getpass()\n",
    "# %sql mysql+pymysql://root:$pwd@localhost:3306/sqlzoo\n",
    "%sql postgresql://postgres:$pwd@localhost/sqlzoo\n",
    "%config SqlMagic.displaylimit = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.\n",
    "How many **stops** are in the database."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>246</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(246,)]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT COUNT(*) FROM stops;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.\n",
    "Find the **id** value for the stop 'Craiglockhart'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "1 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>id</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>53</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(53,)]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT id FROM stops\n",
    "  WHERE name='Craiglockhart';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.\n",
    "Give the **id** and the **name** for the **stops** on the '4' 'LRT' service."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "9 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>id</th>\n",
       "        <th>name</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>19</td>\n",
       "        <td>Bingham</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>53</td>\n",
       "        <td>Craiglockhart</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>85</td>\n",
       "        <td>Fairmilehead</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>115</td>\n",
       "        <td>Haymarket</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>117</td>\n",
       "        <td>Hillend</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>149</td>\n",
       "        <td>London Road</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>177</td>\n",
       "        <td>Northfield</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>179</td>\n",
       "        <td>Oxgangs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>194</td>\n",
       "        <td>Princes Street</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[(19, 'Bingham'),\n",
       " (53, 'Craiglockhart'),\n",
       " (85, 'Fairmilehead'),\n",
       " (115, 'Haymarket'),\n",
       " (117, 'Hillend'),\n",
       " (149, 'London Road'),\n",
       " (177, 'Northfield'),\n",
       " (179, 'Oxgangs'),\n",
       " (194, 'Princes Street')]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT stops.id, stops.name \n",
    "  FROM stops JOIN route ON stops.id=route.stop\n",
    "    WHERE num='4' AND company='LRT';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Routes and stops\n",
    "\n",
    "The query shown gives the number of routes that visit either London Road (149) or Craiglockhart (53). Run the query and notice the two services that link these stops have a count of 2. Add a HAVING clause to restrict the output to these two routes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "2 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>company</th>\n",
       "        <th>num</th>\n",
       "        <th>n_route</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>4</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>45</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('LRT', '4', 2), ('LRT', '45', 2)]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT company, num, COUNT(*) n_route\n",
    "  FROM route WHERE stop=149 OR stop=53\n",
    "    GROUP BY company, num\n",
    "    HAVING COUNT(*)=2;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.\n",
    "Execute the self join shown and observe that b.stop gives all the places you can get to from Craiglockhart, without changing routes. Change the query so that it shows the services from Craiglockhart to London Road.\n",
    "\n",
    "```sql\n",
    "SELECT a.company, a.num, a.stop, b.stop\n",
    "FROM route a JOIN route b ON\n",
    "  (a.company=b.company AND a.num=b.num)\n",
    "WHERE a.stop=53\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "2 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>company</th>\n",
       "        <th>num</th>\n",
       "        <th>start</th>\n",
       "        <th>terminate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>4</td>\n",
       "        <td>53</td>\n",
       "        <td>149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>45</td>\n",
       "        <td>53</td>\n",
       "        <td>149</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('LRT', '4', 53, 149), ('LRT', '45', 53, 149)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT a.company, a.num, a.stop AS start, b.stop terminate\n",
    "FROM route a JOIN route b ON\n",
    "  (a.company=b.company AND a.num=b.num)\n",
    "WHERE a.stop=53 AND b.stop=149;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6.\n",
    "The query shown is similar to the previous one, however by joining two copies of the **stops** table we can refer to **stops** by **name** rather than by number. Change the query so that the services between 'Craiglockhart' and 'London Road' are shown. If you are tired of these places try 'Fairmilehead' against 'Tollcross'\n",
    "\n",
    "```sql\n",
    "SELECT a.company, a.num, stopa.name, stopb.name\n",
    "FROM route a JOIN route b ON\n",
    "  (a.company=b.company AND a.num=b.num)\n",
    "  JOIN stops stopa ON (a.stop=stopa.id)\n",
    "  JOIN stops stopb ON (b.stop=stopb.id)\n",
    "WHERE stopa.name='Craiglockhart'\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "2 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>company</th>\n",
       "        <th>num</th>\n",
       "        <th>start</th>\n",
       "        <th>terminate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>4</td>\n",
       "        <td>Craiglockhart</td>\n",
       "        <td>London Road</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>45</td>\n",
       "        <td>Craiglockhart</td>\n",
       "        <td>London Road</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('LRT', '4', 'Craiglockhart', 'London Road'),\n",
       " ('LRT', '45', 'Craiglockhart', 'London Road')]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT a.company, a.num, stopa.name AS start, stopb.name terminate\n",
    "FROM route a JOIN route b ON\n",
    "  (a.company=b.company AND a.num=b.num)\n",
    "    JOIN stops stopa ON (a.stop=stopa.id)\n",
    "    JOIN stops stopb ON (b.stop=stopb.id)\n",
    "WHERE stopa.name='Craiglockhart' AND stopb.name='London Road';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. [Using a self join](https://sqlzoo.net/wiki/Using_a_self_join)\n",
    "\n",
    "Give a list of all the services which connect stops 115 and 137 ('Haymarket' and 'Leith')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "6 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>company</th>\n",
       "        <th>num</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>2A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>SMT</td>\n",
       "        <td>C5</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('LRT', '12'),\n",
       " ('LRT', '2'),\n",
       " ('LRT', '22'),\n",
       " ('LRT', '25'),\n",
       " ('LRT', '2A'),\n",
       " ('SMT', 'C5')]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT DISTINCT a.company, a.num FROM\n",
    "route a JOIN route b ON\n",
    "  (a.company=b.company AND a.num=b.num)\n",
    "WHERE a.stop=115 AND b.stop=137;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8.\n",
    "Give a list of the services which connect the stops 'Craiglockhart' and 'Tollcross'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "4 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>company</th>\n",
       "        <th>num</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>LRT</td>\n",
       "        <td>47</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('LRT', '10'), ('LRT', '27'), ('LRT', '45'), ('LRT', '47')]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT a.company, a.num\n",
    "FROM route a JOIN route b ON\n",
    "  (a.company=b.company AND a.num=b.num)\n",
    "    JOIN stops stopa ON (a.stop=stopa.id)\n",
    "    JOIN stops stopb ON (b.stop=stopb.id)\n",
    "WHERE stopa.name='Craiglockhart' AND stopb.name='Tollcross';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.\n",
    "Give a distinct list of the **stops** which may be reached from 'Craiglockhart' by taking one bus, including 'Craiglockhart' itself, offered by the LRT company. Include the company and bus no. of the relevant services."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "46 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>stop</th>\n",
       "        <th>company</th>\n",
       "        <th>num</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Balerno Church</td>\n",
       "        <td>LRT</td>\n",
       "        <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Princes Street</td>\n",
       "        <td>LRT</td>\n",
       "        <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Currie</td>\n",
       "        <td>LRT</td>\n",
       "        <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Canonmills</td>\n",
       "        <td>LRT</td>\n",
       "        <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>London Road</td>\n",
       "        <td>LRT</td>\n",
       "        <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Fairmilehead</td>\n",
       "        <td>LRT</td>\n",
       "        <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Riccarton Campus</td>\n",
       "        <td>LRT</td>\n",
       "        <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Tollcross</td>\n",
       "        <td>LRT</td>\n",
       "        <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Craiglockhart</td>\n",
       "        <td>LRT</td>\n",
       "        <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Tollcross</td>\n",
       "        <td>LRT</td>\n",
       "        <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Hunters Tryst</td>\n",
       "        <td>LRT</td>\n",
       "        <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Leith</td>\n",
       "        <td>LRT</td>\n",
       "        <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Torphin</td>\n",
       "        <td>LRT</td>\n",
       "        <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Duddingston</td>\n",
       "        <td>LRT</td>\n",
       "        <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Craiglockhart</td>\n",
       "        <td>LRT</td>\n",
       "        <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Craiglockhart</td>\n",
       "        <td>LRT</td>\n",
       "        <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Hillend</td>\n",
       "        <td>LRT</td>\n",
       "        <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Balerno</td>\n",
       "        <td>LRT</td>\n",
       "        <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Craiglockhart</td>\n",
       "        <td>LRT</td>\n",
       "        <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Muirhouse</td>\n",
       "        <td>LRT</td>\n",
       "        <td>10</td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">46 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[('Balerno Church', 'LRT', '47'),\n",
       " ('Princes Street', 'LRT', '10'),\n",
       " ('Currie', 'LRT', '45'),\n",
       " ('Canonmills', 'LRT', '27'),\n",
       " ('London Road', 'LRT', '45'),\n",
       " ('Fairmilehead', 'LRT', '4'),\n",
       " ('Riccarton Campus', 'LRT', '45'),\n",
       " ('Tollcross', 'LRT', '45'),\n",
       " ('Craiglockhart', 'LRT', '10'),\n",
       " ('Tollcross', 'LRT', '47'),\n",
       " ('Hunters Tryst', 'LRT', '27'),\n",
       " ('Leith', 'LRT', '10'),\n",
       " ('Torphin', 'LRT', '10'),\n",
       " ('Duddingston', 'LRT', '45'),\n",
       " ('Craiglockhart', 'LRT', '27'),\n",
       " ('Craiglockhart', 'LRT', '47'),\n",
       " ('Hillend', 'LRT', '4'),\n",
       " ('Balerno', 'LRT', '47'),\n",
       " ('Craiglockhart', 'LRT', '45'),\n",
       " ('Muirhouse', 'LRT', '10'),\n",
       " ('Silverknowes', 'LRT', '27'),\n",
       " ('Cockburn Crescent', 'LRT', '47'),\n",
       " ('Colinton', 'LRT', '45'),\n",
       " ('Leith Walk', 'LRT', '10'),\n",
       " ('Craiglockhart', 'LRT', '4'),\n",
       " ('Silverknowes', 'LRT', '10'),\n",
       " ('Hanover Street', 'LRT', '47'),\n",
       " ('Hanover Street', 'LRT', '27'),\n",
       " ('Oxgangs', 'LRT', '4'),\n",
       " ('Bingham', 'LRT', '4'),\n",
       " ('Tollcross', 'LRT', '10'),\n",
       " ('Northfield', 'LRT', '4'),\n",
       " ('Oxgangs', 'LRT', '27'),\n",
       " ('Colinton', 'LRT', '47'),\n",
       " ('Northfield', 'LRT', '45'),\n",
       " ('Crewe Toll', 'LRT', '27'),\n",
       " ('Currie', 'LRT', '47'),\n",
       " ('Hanover Street', 'LRT', '45'),\n",
       " ('Colinton', 'LRT', '10'),\n",
       " ('Brunstane', 'LRT', '45'),\n",
       " ('Haymarket', 'LRT', '4'),\n",
       " ('London Road', 'LRT', '4'),\n",
       " ('Newhaven', 'LRT', '10'),\n",
       " ('Tollcross', 'LRT', '27'),\n",
       " ('Princes Street', 'LRT', '4'),\n",
       " ('Canonmills', 'LRT', '47')]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT DISTINCT stopb.name stop, a.company, a.num\n",
    "FROM route a JOIN route b ON\n",
    "  (a.company=b.company AND a.num=b.num)\n",
    "    JOIN stops stopa ON (a.stop=stopa.id)\n",
    "    JOIN stops stopb ON (b.stop=stopb.id)\n",
    "WHERE stopa.name='Craiglockhart' AND a.company='LRT';"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.\n",
    "Find the routes involving two buses that can go from **Craiglockhart** to **Lochend**.\n",
    "Show the bus no. and company for the first bus, the name of the stop for the transfer,\n",
    "and the bus no. and company for the second bus.\n",
    "\n",
    "> _Hint_    \n",
    "> Self-join twice to find buses that visit Craiglockhart and Lochend, then join those on matching stops."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * postgresql://postgres:***@localhost/sqlzoo\n",
      "36 rows affected.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>num1</th>\n",
       "        <th>company1</th>\n",
       "        <th>transfer</th>\n",
       "        <th>num2</th>\n",
       "        <th>company2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Princes Street</td>\n",
       "        <td>C5</td>\n",
       "        <td>SMT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Princes Street</td>\n",
       "        <td>65</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Duddingston</td>\n",
       "        <td>46A</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Duddingston</td>\n",
       "        <td>42</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>London Road</td>\n",
       "        <td>87A</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>London Road</td>\n",
       "        <td>C5</td>\n",
       "        <td>SMT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>London Road</td>\n",
       "        <td>20</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>London Road</td>\n",
       "        <td>34</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>London Road</td>\n",
       "        <td>46A</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>London Road</td>\n",
       "        <td>87</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>London Road</td>\n",
       "        <td>42</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>London Road</td>\n",
       "        <td>35</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>45</td>\n",
       "        <td>LRT</td>\n",
       "        <td>London Road</td>\n",
       "        <td>65</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>47</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Canonmills</td>\n",
       "        <td>34</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>47</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Canonmills</td>\n",
       "        <td>35</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Princes Street</td>\n",
       "        <td>C5</td>\n",
       "        <td>SMT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>4</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Princes Street</td>\n",
       "        <td>65</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Leith</td>\n",
       "        <td>87</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Leith</td>\n",
       "        <td>34</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>10</td>\n",
       "        <td>LRT</td>\n",
       "        <td>Leith</td>\n",
       "        <td>35</td>\n",
       "        <td>LRT</td>\n",
       "    </tr>\n",
       "</table>\n",
       "<span style=\"font-style:italic;text-align:center;\">36 rows, truncated to displaylimit of 20</span>"
      ],
      "text/plain": [
       "[('10', 'LRT', 'Princes Street', 'C5', 'SMT'),\n",
       " ('10', 'LRT', 'Princes Street', '65', 'LRT'),\n",
       " ('45', 'LRT', 'Duddingston', '46A', 'LRT'),\n",
       " ('45', 'LRT', 'Duddingston', '42', 'LRT'),\n",
       " ('45', 'LRT', 'London Road', '87A', 'LRT'),\n",
       " ('45', 'LRT', 'London Road', 'C5', 'SMT'),\n",
       " ('45', 'LRT', 'London Road', '20', 'LRT'),\n",
       " ('45', 'LRT', 'London Road', '34', 'LRT'),\n",
       " ('45', 'LRT', 'London Road', '46A', 'LRT'),\n",
       " ('45', 'LRT', 'London Road', '87', 'LRT'),\n",
       " ('45', 'LRT', 'London Road', '42', 'LRT'),\n",
       " ('45', 'LRT', 'London Road', '35', 'LRT'),\n",
       " ('45', 'LRT', 'London Road', '65', 'LRT'),\n",
       " ('47', 'LRT', 'Canonmills', '34', 'LRT'),\n",
       " ('47', 'LRT', 'Canonmills', '35', 'LRT'),\n",
       " ('4', 'LRT', 'Princes Street', 'C5', 'SMT'),\n",
       " ('4', 'LRT', 'Princes Street', '65', 'LRT'),\n",
       " ('10', 'LRT', 'Leith', '87', 'LRT'),\n",
       " ('10', 'LRT', 'Leith', '34', 'LRT'),\n",
       " ('10', 'LRT', 'Leith', '35', 'LRT'),\n",
       " ('10', 'LRT', 'Leith', 'C5', 'SMT'),\n",
       " ('4', 'LRT', 'Haymarket', 'C5', 'SMT'),\n",
       " ('4', 'LRT', 'Haymarket', '65', 'LRT'),\n",
       " ('27', 'LRT', 'Crewe Toll', '20', 'LRT'),\n",
       " ('45', 'LRT', 'Riccarton Campus', '65', 'LRT'),\n",
       " ('27', 'LRT', 'Canonmills', '34', 'LRT'),\n",
       " ('27', 'LRT', 'Canonmills', '35', 'LRT'),\n",
       " ('4', 'LRT', 'London Road', '87A', 'LRT'),\n",
       " ('4', 'LRT', 'London Road', 'C5', 'SMT'),\n",
       " ('4', 'LRT', 'London Road', '20', 'LRT'),\n",
       " ('4', 'LRT', 'London Road', '34', 'LRT'),\n",
       " ('4', 'LRT', 'London Road', '46A', 'LRT'),\n",
       " ('4', 'LRT', 'London Road', '87', 'LRT'),\n",
       " ('4', 'LRT', 'London Road', '42', 'LRT'),\n",
       " ('4', 'LRT', 'London Road', '35', 'LRT'),\n",
       " ('4', 'LRT', 'London Road', '65', 'LRT')]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT num1, company1, transfer, num2, company2 FROM\n",
    "(SELECT DISTINCT stopb.name transfer, a.company company1, a.num num1, b.stop stopid\n",
    " FROM route a JOIN route b ON\n",
    " (a.company=b.company AND a.num=b.num)\n",
    " JOIN stops stopa ON (a.stop=stopa.id)\n",
    " JOIN stops stopb ON (b.stop=stopb.id)\n",
    " WHERE stopa.name='Craiglockhart') bus1 JOIN\n",
    "(SELECT DISTINCT b.company company2, b.num num2, a.stop stopid\n",
    " FROM route a JOIN route b ON\n",
    " (a.company=b.company AND a.num=b.num)\n",
    " JOIN stops stopa ON (a.stop=stopa.id)\n",
    " JOIN stops stopb ON (b.stop=stopb.id)\n",
    " WHERE stopb.name='Lochend') bus2\n",
    "ON bus1.stopid=bus2.stopid;"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
